package com.fingard.dsp.bank.directbank.bocom13;

import com.fingard.dsp.bank.directConfig.ReceiptProConfig;
import com.fingard.dsp.bank.directbank.DirectHelper;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IReceiptPro;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.ReqQryReceipt;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.RetAtsReceipt;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

public class BOCOM13ReceiptPro extends BOCOM13Base implements IReceiptPro {
    @Override
    public RetAtsReceipt qryReceiptOneDay(ReceiptProConfig pConfig, ReqQryReceipt pReqReceipt, Date pOneDay) throws Exception {
        RetAtsReceipt retAts = new RetAtsReceipt(pReqReceipt.reqHeader);
        while(pReqReceipt.readRecord()) {
            BOCOM13Header tmpBankHeader = new BOCOM13Header();
            tmpBankHeader.transCode = "I00406";
            tmpBankHeader.reqSeqID = getQryTransID();
            String corpNo = getBankConfig().getMapValueByKey("corpNoMapping", pReqReceipt.actNum);
            if (StringHelper.isNullOrEmpty(corpNo)) {
                retAts.respCode =  "-1";
                retAts.respInfo = "未找到账号[" + pReqReceipt.actNum + "]保险方ID映射,请检查BOCOM13/config.xml中'corpNoMapping'节点配置";
                retAts.writeRetToATS();
                return retAts;
            }
            tmpBankHeader.corpNo = corpNo;
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, tmpBankHeader);
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("BusSeqNo", "");
            xmlWriter.writeElementString("EntSeqNo", pReqReceipt.readedDetail.askNumber);
            xmlWriter.writeElementString("EntBusDate", pReqReceipt.readedDetail.payDate);
            xmlWriter.writeElementString("Ext001", "");
            xmlWriter.writeElementString("Ext002", "");
            xmlWriter.writeElementString("Ext003", "");
            xmlWriter.writeElementString("Ext004", "");
            xmlWriter.writeElementString("Ext005", "");
            xmlWriter.writeElementString("Ext006", "");
            xmlWriter.writeElementString("Ext007", "");
            xmlWriter.writeElementString("Ext008", "");
            xmlWriter.writeElementString("Ext009", "");
            xmlWriter.writeElementString("Ext010", "");
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankFront.serverPort);

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    retAts.respCode = "0";
                    String tmpErrMsg=tmpBkReader.getFirstText("/Rsp/Head/ErrMsg");
                    retAts.respInfo = tmpErrMsg;
                    String tmpMsgType = tmpBkReader.getFirstText("/Rsp/Head/MsgType");
                    String tmpErrCode = tmpBkReader.getFirstText("/Rsp/Head/ErrCode");
                    if ("N".equals(tmpMsgType) && "000000".equals(tmpErrCode)) {
                        String receiptFileName = tmpBkReader.getFirstText("/Rsp/Body/ReturnFileName");
                        // 将流水号和银行回单文件名关联保存至db
                        StringBuilder sbLog = new StringBuilder();
                        boolean saved = DirectHelper.saveReqSeqIDMapping(pReqReceipt.reqHeader.bankCode,pReqReceipt.reqHeader.transType, new Date(), receiptFileName, pReqReceipt.readedDetail.askNumber,sbLog);
                        if(!saved) {
                            retAts.respCode = "-1";
                            retAts.respInfo = sbLog.toString();
                        }
                    }else {
                        retAts.respCode = "-1";
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        }

        retAts.writeRetToATS();
        return retAts;
    }
}